//
//  LGTRechargeVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/10.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTRechargeVC: LGTBaseVC, UITextViewDelegate{
    var buttonArray:Array<UIButton> = []
    //var textView:UITextView?
    var moneyTextView:UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //微信支付
        NotificationCenter.default.addObserver(self, selector: #selector(paySuccessAction), name: WXPay_ReCharge_Note, object: nil)
    }
    
    @objc func paySuccessAction(note:Notification){
        var viewContollers = self.navigationController!.viewControllers
        viewContollers.removeLast()
        let vc:LGTRechargeSuccessVC = LGTRechargeSuccessVC()
        viewContollers.append(vc)
        self.navigationController!.viewControllers = viewContollers
        self.navigationController?.popToViewController(vc, animated: false)
        //self.navigationController?.pushViewController(vc, animated: true)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: WXPay_ReCharge_Note, object: nil)
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "充值", back: true)
        self.view.backgroundColor = UIColor.white
        let topView:UIView = UIView.init()
        self.view.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(NavBarH())
            make.height.equalTo(SizeFit(126))
        }
        
        let label:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(18), text: "充值金额", corner: 0)
        topView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(14))
            make.top.equalTo(SizeFit(24))
        }
        
        self.moneyTextView = LGTViewCreateUtil.share.createTextFied(font: KFont(size: SizeFitFloat(30)), keyBoardType: UIKeyboardType.decimalPad, placeholder: "0.00", textColor: ColorUtil(color: 0xF2421F))
        topView.addSubview(self.moneyTextView!)
        self.moneyTextView!.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFit(30))
            make.left.equalTo(SizeFit(13))
            make.width.equalTo(SizeFit(300))
            make.top.equalTo(SizeFit(72))
        }
        
        let leftView:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: ColorUtil(color: 0xF2421F), fontSize: SizeFitFloat(30), text: "¥")
        leftView.frame = CGRect.init(x: 0, y: 0, width: SizeFit(30), height: SizeFit(30))
        self.moneyTextView?.leftView = leftView
        self.moneyTextView?.leftViewMode = UITextField.ViewMode.always
        
        let line:UIView = UIView.init()
        line.backgroundColor = ViewBackColor
        topView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.bottom.left.right.equalTo(0)
            make.height.equalTo(SizeFit(9))
        }
        
        let payWayLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(18), text: "请选择付款方式")
        self.view.addSubview(payWayLabel)
        payWayLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(147) + NavBarH())
        }
        
        let array:Array<Dictionary<String, Any>> = [
                                                    ["title":"微信支付", "icon":"wechatpayment", "type":1],
//                                                    ["title":"支付宝支付", "icon":"alipaypayment", "type":2]]
            ]
        
        for i in 0..<array.count{
            let d:Dictionary<String, Any> = array[i]
            let image:String = d["icon"] as! String
            let title:String = d["title"] as! String
            let imageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: image)
            self.view.addSubview(imageView)
            imageView.snp.makeConstraints { (make) in
                make.left.equalTo(SizeFit(13))
                make.height.width.equalTo(SizeFit(20))
                make.top.equalTo(SizeFit(Double(191 + i * 43) + NavBarH()))
            }
            
            let payTitleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(18), text: title)
            self.view.addSubview(payTitleLabel)
            payTitleLabel.snp.makeConstraints { (make) in
                make.left.equalTo(SizeFit(53))
                make.height.equalTo(18)
                make.centerY.equalTo(imageView)
            }
            
            let button:UIButton = LGTViewCreateUtil.share.createLGTButton(image: "unchecked", selected: "selection", dW: SizeFitFloat(5), dH: SizeFitFloat(5))
            self.view.addSubview(button)
            button.isSelected = true
            button.snp.makeConstraints { (make) in
                make.right.equalTo(-SizeFit(22))
                make.width.height.equalTo(SizeFit(20))
                make.centerY.equalTo(imageView)
            }
            
            let type:Int = d["type"] as? Int ?? 0
            button.tag = type
            button.addTarget(self, action: #selector(choseAction), for: UIControl.Event.touchUpInside)
            self.buttonArray.append(button)
        }
        
        
        let textView:UITextView = LGTViewCreateUtil.share.createSpecialTextView(agreenStr: "充值须知：充值额度最低限制为1元，详情请参阅《发单规则》", specialStr: "《发单规则》", normalColor: GrayColor, speailColor: YellowColor, font: Font_Normal_12, textAlignment: NSTextAlignment.left)
        self.view.addSubview(textView)
        textView.delegate = self
        textView.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(17))
            make.height.equalTo(SizeFit(30))
            make.top.equalTo(topView.snp_bottomMargin).offset(SizeFit(103))
        }
        
        
        let sureButton:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "确认支付", font: Font_Normal_18, color: UIColor.white, selectedColor: UIColor.white, corner: SizeFitFloat(22), backColor: YellowColor)
        self.view.addSubview(sureButton)
        sureButton.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(25))
            make.right.equalTo(SizeFitFloat(-21))
            make.height.equalTo(SizeFitFloat(44))
            make.top.equalTo(topView.snp_bottomMargin).offset(SizeFit(183))
        }
        sureButton.addTarget(self, action: #selector(sureAction), for: UIControl.Event.touchUpInside)
    }
    
    @objc func choseAction(sender:UIButton){
        
    }
    
    @objc func sureAction(){
        if self.moneyTextView?.text?.count == 0{
            WisdomHUD.showError(text: "请输入充值金额")
            return
        }
        
        let money:Float = Float(self.moneyTextView?.text ?? "") ?? 0
        if money < 1{
            WisdomHUD.showError(text: "充值额度最低限制为1元")
            return
        }
        
        //转分
        let moneyMin = Int(money * 100)
        let param = ["amount":moneyMin]
//        "appid": "string",
//        "partnerid": "string",
//        "prepayid": "string",
//        "package": "string",
//        "noncestr": "string",
//        "timestamp": "string",
//        "sign": "string"
        LGTHttpUtil.httpUtil.requestData(url: WX_Recharge_URL, param: param, success: { (json) in
            
            let data:Dictionary<String, Any> = json["data"] as? Dictionary<String, Any> ?? [:]
            LGTViewUtil.share.wxPay(data: data)
//            let model:WXPayModel = try! lgtDecode(WXPayModel.self, param: (data))
//            let payReq:PayReq = PayReq.init()
//            payReq.prepayId = model.prepayid
//            payReq.openID = model.appid
//            payReq.partnerId = model.partnerid
//            payReq.nonceStr = model.noncestr
//            payReq.timeStamp = model.timestamp!
//            payReq.package = model.package
//            payReq.sign = model.sign
//            WXApi.send(payReq)
        }) { (json) in
            
        }
    }
    
    //点击跳转
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        let url = Html_URL(Public_Order_Html_URL) 
        let webVC = LGTBaseWebVC()
        webVC.url = url
        self.navigationController?.pushViewController(webVC, animated: true)
        return false
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}
